<extend name="./public/frame.html"/>
<block name="content">
    <style>
        .layui-form-label{width: 12%;}
        .layui-input-block{margin-left: 12%;}
    </style>
    <form class="layui-form layui-form-pane1" method="post" enctype="multipart/form-data">
        <table class="layui-table">
            <tr>
                <td style="text-align: center;width: 12%">类型</td>
                <td>
                    <input type="radio" name="type" value="2" title="司机" checked>
                    <!--<input type="radio" name="type" value="1" title="乘客">-->
                </td>
            </tr>

            <tr>
                <td style="text-align: center;width: 12%">短信模板</td>
                <td>
                    <select id="molSms" class="layui-input" lay-filter="molSms">
                        <?php foreach($SmsTyep as $key=>$value):?>
                        <option value="<?php echo $value['id'];?>"><?php echo $value['name'];?></option>
                        <?php endforeach;?>
                    </select>
                </td>
            </tr>
            <tr>
                <td style="text-align: center;width: 12%">短信模板内容</td>
                <td>
                    <?php foreach($SmsTyep as $key1=>$value1):?>
                    <div class="molSms molSms<?php echo $value1['id'];?>" <?php if($key1>1){ echo "style='display: none'";}?>><?php echo $value1['content'];?></div>
                    <?php endforeach;?>

                </td>
            </tr>
            <!--<tr>
                <td style="text-align: center;width: 12%">时间</td>
                <td>
                    <input type="text" id="times" name="times" value="" class="layui-input" style="width: 300px;float: left;">
                </td>
            </tr>
            <tr>
                <td style="text-align: center;width: 12%">平台名称</td>
                <td>
                    <input type="text" id="names" name="names" value="" class="layui-input" style="width: 300px;float: left;">
                </td>
            </tr>-->
            <?php foreach($SmsTyep as $key2=>$value2):?>
            <?php if($value2['is_p']==1):?>
            <?php foreach($value2['parameter'] as $key3=>$value3):?>
            <tr class="molSms molSms<?php echo $value2['id'];?>" <?php if($key2>1){ echo "style='display: none'";}?>>
                <td style="text-align: center;width: 12%"><?php echo $value3['name'];?></td>
                <td>
                    <input type="text" name="smsData_<?php echo $value3['parameter'];?>" value="" class="layui-input" style="width: 300px;float: left;">
                </td>
            </tr>
            <?php endforeach;?>
            <?php endif;?>
            <?php endforeach;?>

            <tr>
                <td style="text-align: center;width: 12%">地区</td>
                <td style="width: 20%">
                    <select name="province" class="layui-input" lay-filter="region" lay-verify="required">
                        <option value="">--请选择省--</option>
                        <volist name="provinces" id="province">
                            <option value="{$province['region_id']}">{$province['region_name']}</option>
                        </volist>
                    </select>
                </td>
                <td style="width: 20%">
                    <select name="city" class="layui-input" lay-filter="region" lay-verify="required">
                        <option value="">--请选择市--</option>
                    </select>
                </td>
                <td style="display: none">
                    <select name="area" class="layui-input" lay-filter="region">
                        <option value="">--请选择县/区(可不选)--</option>
                    </select>
                </td>

            </tr>
            <tr>
                <td colspan="2" style="padding-left: 100px;">
                    <button class="layui-btn" lay-submit lay-filter="*">提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </td>
                <td colspan="2" style="padding-left: 100px;">
                    <div id="statusSmsMass"></div>
                </td>
            </tr>
        </table>
    </form>
    <script>
        layui.use(['form'], function () {
            var form = layui.form;
            form.on('submit(*)', function(data){
                $('#statusSmsMass').html('');
                $.ajax({
                    url:'{:url("index")}',
                    async:true,
                    dataType:'json',
                    type:'POST',
                    data: $(data.form).serialize(),
                    success:function(res){
                        var smsData= {};
                        $("input[name*='smsData']").each(function(i, el) {
                            console.log(i);
                            console.log(el);
                            console.log(el.name);
                            console.log($(this).val());
                            var str=el.name;
                            var arr=str.split('_');
                            smsData[arr[1]] =$(this).val();
                        });
                        console.log(smsData);

                        if(res.code==1){
                            layer.confirm('确定发送吗?（'+res.count+'条)',function(index){
                                /*var times=$('#times').val();
                                var names=$('#names').val();
                                var phones=$('#phones').val();
                                //,'times': times,'names': names,'phones': phones*/
                                var molSms=$('#molSms').val();
                                $.ajax({
                                    url:'{:url("postSmsMass")}',
                                    async:true,
                                    dataType:'json',
                                    type:'POST',
                                    data: {'phoneArr': res.data,'smsData': smsData,'molSmsTyep': molSms},
                                    success:function(resData){
                                        console.log(resData);
                                        if(resData.status == 1){
                                            $('#statusSmsMass').html('<span style="color: green">发送成功</span>');
                                            layer.msg(resData.msg,{'icon':1});
                                            //window.location.reload();
                                        }else{
                                            $('#statusSmsMass').html('<span style="color: red">发送失败</span>');
                                            layer.msg(resData.msg,{'icon':2});
                                        }
                                    }
                                });
                                layer.close(index);
                                //window.location.reload();
                            })
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                return false;
            });
            form.on('select(region)', function(data){
                var name = $(data.elem).attr('name');
                var parent_id = data.value;
                if(name==='area'){
                    var index = name == 'town';
                    ajax_town(parent_id,index,form);
                    return;
                }
                if(name==='town'){
                    return;
                }
                var index = name === 'province' ? 'city' : 'area';
                ajax(parent_id,index,form);
            });
            form.on('select(molSms)', function(data){
                var parent_id = data.value;
                $('.molSms').hide();
                var str='.molSms'+parent_id;
                $(str).show();
            });
        });
        function ajax(parent_id,index,form) {
            var op = index==='city'?'市':'县/区(可不选)';
            var level = index==='city'?3:4;
            var select = $("select[name="+index+"]");
            $.post("{:url('getRegion')}",{parent_id:parent_id,level:level},function (re) {
                if(re['status']==1){
                    var str = "<option value=''>--请选择"+op+"--</option>";
                    re['data'].forEach(function (item,index,input) {
                        str += "<option code='"+item['region_code']+"' value='"+item['region_id']+"'>"+item['region_name']+"</option>";
                    });
                    select.html(str);
                    form.render('select');
                }
            },'json')
        }
        function ajax_town(parent_id,index,form) {
            var op = '乡/镇(可不选)';

            var select = $("select[name=town]");

            $.post("{:url('getTown')}",{parent_id:parent_id},function (re) {
                console.log(re)
                if(re['status']==1){
                    var str = "<option value=''>--请选择"+op+"--</option>";
                    re['data'].forEach(function (item,index,input) {
                        str += "<option value='"+item['town_code']+"'>"+item['name']+"</option>";
                    });
                    select.html(str);
                    form.render('select');
                }
            })
        }
    </script>
</block>
